package com.tjlg.auto;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tjlg.entity.WaUser;
import com.tjlg.mapper.WaUserMapper;
import com.tjlg.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录成功操作
 */
@Component
public class MyAuthenticationSuccessHandler extends JSONAuthentication implements AuthenticationSuccessHandler {
@Autowired
WaUserMapper waUserMapper;
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("username",SecurityContextHolder.getContext().getAuthentication().getName());
        WaUser exUser = waUserMapper.selectOne(wrapper);
        Result result = Result.ok().message("登录成功").data("user",exUser);
        this.WriteJSON(request,response,result);
    }
}